//app.js
App({
  onLaunch: function () {
    var that=this;
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        var code = res.code;
        console.log(res)
        if (res.code) {
          wx.getUserInfo({
            success: function (res) {
              // userInfo 只存储个人的基础数据
              wx.setStorageSync('userInfo', res.userInfo);
              // 只获取openid的话，自己就可以
              that.getOpenid(code);
            }
          })
        } else {
          console.log('获取用户登录态失败！' + res.errMsg)
        }
      }
    })
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              console.log(res);
              this.globalData.userInfo = res.userInfo
              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    })
  },
  getOpenid: function (code) {
    var that = this;
    wx.request({
      url: that.globalData.api_url +'/OpenAPI/V1/MiniWx/getSessionKey',
      data: { 
          code:code
      },
      method: 'GET',
      success: function (res) {
        console.log(res)
        console.log(res.data.data.openid)
        that.miniLogin(res.data.data.openid)
      }
    });
  },
  globalData: {
    userInfo: null,
    machine_id:null,
    devurl:"http://xiaochengxu-1251151699.coscd.myqcloud.com",
    api_url:"https://api.mxzhuawawa.com",
  },
  user:{
    nickname: "183****2844",
    avatar: "http://api.mxzhuawawa.com/style/app/avatar.png",
    coinbalance: "12874",
    uid: "14701",
    token:"a289aea20331c225f6d90f256af7037e"
  },
  backpack_record:{
    backpack_order:[],
  },
  choice_address:{
    name:"",
    phone:"",
    region:"",
    address:""
  },
  miniLogin:function(openid){
    var that=this;
    wx.getStorage({
      key: 'userInfo',
      success: function (res) {
        console.log(res.data)
        var userinfo={
          unionid: openid,
          headimgurl: res.data.avatarUrl,
          city: res.data.city,
          country: res.data.country,
          sex: res.data.gender,
          language: res.data.language,
          province: res.data.province,
          nickname: res.data.nickName,
          openid: openid
        }
        wx.request({
          url: that.globalData.api_url + "/OpenAPI/v1/auth/login",
          data: {
            "openid": openid,
            "type": "wechat",
            "payload": userinfo
          },
          method: 'GET',
          success: function (respone) {
            console.log(respone)
            that.globalData.userInfo = respone.data;
            console.log(that.globalData.userInfo)
          }
        })
      }
    })
    
  }
})